package utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import model.DefaultParams;

public class BeanUtils 
{
	public static void transform(Object object, String field, Object params)
	{
		try 
		{
			Class clz = object.getClass();
			Field f = clz.getDeclaredField(field);
			f.setAccessible(true);
			f.set(object, params);
			f.setAccessible(false);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	public static void strutsDefaultParamsTransform(DefaultParams dp, Map<String, String> map) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{
		Class clz = dp.getClass();
		Field field[] = clz.getDeclaredFields();
		for(int i = 0; i < field.length; i++)
		{
			Field f = field[i];
			String fieldName = f.getName();
			Method method = clz.getDeclaredMethod("get" + (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1), null);
			String params = (String) method.invoke(dp, null);
			if(!StringUtils.isBlank(params))
			{
				map.remove(fieldName);
				map.put(fieldName, params);
			}
		}
	}
}
